package com.ujs.edu.demo.controller;

import com.ujs.edu.demo.config.NeedLogin;
import com.ujs.edu.demo.dao.BcRoleMapper;
import com.ujs.edu.demo.dataobject.BcRole;
import com.ujs.edu.demo.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
public class RoleController {

    @Autowired
    private BcRoleMapper bcRoleMapper;

    //显示角色管理窗口

    @RequestMapping(path = "/role")
    public ModelAndView role(){
        ModelAndView view=new ModelAndView();
        List<BcRole> bcRoles=bcRoleMapper.selectAll();

        view.addObject("roles",bcRoles);
        view.setViewName("role");
        return view;
    }

    //添加角色
    @RequestMapping(path = "/addRole")
    @ResponseBody
    public Result addRole(BcRole bcRole){
        Result result=new Result();
        BcRole bcRole1=bcRoleMapper.selectByPrimaryKey(bcRole.getRoleId());
        System.out.println(bcRole.getRoleId());
       if (bcRole1!=null){
           result.setStatus(0);
           result.setMsg("已经存在该角色");
       }else {
           int bc=bcRoleMapper.insert(bcRole);
           if (bc==1){
               result.setStatus(1);
               result.setMsg("插入成功");
           }else {
               result.setStatus(0);
               result.setMsg("失败");
           }
       }
       return result;
    }



    //回填需要修改的角色的信息
    @RequestMapping(path = "/putRole")
    public ModelAndView modRole(@RequestParam("roleId") Integer roleId){
        ModelAndView view=new ModelAndView();

        BcRole bcRole=bcRoleMapper.selectByPrimaryKey(roleId);
        view.addObject("role",bcRole);
        view.setViewName("modrole");
        return  view;
    }

    //提交修改
    @RequestMapping(path = "/modRole")
    @ResponseBody
    public  Result modRole(BcRole bcRole){
        Result result=new Result();
        System.out.println(bcRole.getRoleId());
        int bc=bcRoleMapper.updateByPrimaryKey(bcRole);

        if (bc==1){
            result.setStatus(1);
            result.setMsg("成功");
        }else {
            result.setStatus(0);
            result.setMsg("失败");
        }

        return result;
    }

    //删除角色
    @RequestMapping(path = "/delRole")
    @ResponseBody
    public Result delRole(@RequestParam("roleId") Integer roleId){
        Result result=new Result();

        int rol=bcRoleMapper.deleteByPrimaryKey(roleId);
        if (rol==1){
            result.setStatus(1);
            result.setMsg("删除成功");
        }else {
            result.setStatus(0);
            result.setMsg("删除失败");
        }
        return result;
    }
}
